import {
  getSetting,
  chooseAddress,
  openSetting,
  showModal,
  showToast,
} from "../../utils/asyncWx";
Page({
  /**
   * 获取用户收货地址
   *   1:绑定个点击事件
   *   2:调用小程序的内置api, 获取再微信里面设置的收货地址;
   */
  data: {
    address: {},
    cart: [],
    allChecked: false,
    totalPrice: 0,
    totalNum: 0,
  },
  onShow() {
    //1:获取缓存中的收货地址信息;
    const address = wx.getStorageSync("address");
    //获取缓存中购物车 的数据;
    const cart = wx.getStorageSync("cart") || [];
    this.setData({
      address,
    });
    //调用自定义的方法setcart去更新购物车的数据
    this.setCart(cart);
  },
  //获取收货地址
  async handleChooseAddress() {
    try {
      // 1 获取权限状态
      const res1 = await getSetting();
      // 调用上面这个方法的时候,获取用户曾经是否设置过获取收货地址的权限,
      // 如果点击了确定. authSetting scopeAddress 的true-----直接获取收货地址;
      // 如果用户从来没有调用过  scope的值是undefined  -----没有获取
      // 如果点击了取消 ---scope的值就false
      //  1:诱导用户去打开授权设置的界面----opensetting----- 可以给获取收货地址授权;

      //表示看看用户有没有获取收货地址的权限;
      const scopeAddress = res1.authSetting["scope.address"];
      if (scopeAddress === false) {
        //打开权限设置的页面; ---让用户重新设置权限;
        await openSetting();
      }

      // 调用收货地址的api
      let address = await chooseAddress();
      console.log(address);
      address.all =
        address.provinceName +
        address.cityName +
        address.countyName +
        address.detailInfo;

      //存到缓存中
      wx.setStorageSync("address", address);
    } catch (error) {
      console.log("你干哈呢");
    }
  },
  // 商品的选中状态发生变化的时候;
  handeItemChange(e) {
    // 先去获取被修改的商品的id
    const goods_id = e.currentTarget.dataset.id;
    //获取购物车数组;
    let { cart } = this.data;
    // 找到对应被修改的商品的对象;
    let index = cart.findIndex((v) => v.goods_id === goods_id);
    //选中状态取反
    cart[index].checked = !cart[index].checked;
    this.setCart(cart);
  },
  setCart(cart) {
    //
    let allChecked = true;
    // 总价格和总数量;
    let totalPrice = 0;
    let totalNum = 0;
    cart.forEach((v) => {
      if (v.checked) {
        totalPrice += v.num * v.goods_price;
        totalNum += v.num;
      } else {
        allChecked = false;
      }
    });

    allChecked = cart.length != 0 ? allChecked : false;

    this.setData({
      cart,
      totalPrice,
      totalNum,
      allChecked,
    });
    wx.setStorageSync("cart", cart);
  },
  //商品全选功能
  handleItemAllCheck() {
    //获取data里面定义的数据
    let { cart, allChecked } = this.data;
    //console.log(allChecked)
    //修改值
    allChecked = !allChecked;
    //   console.log(allChecked)
    //修改cart中每个商品的选择信息;
    cart.forEach((v) => {
      v.checked = allChecked;
    });

    // console.log(cart)
    //修改之后的数据给重新存入到data或者缓存中
    this.setCart(cart);
  },
  //商品加减
  async handleItemNumEdit(e) {
    //取过来参数
    let { operation, id } = e.currentTarget.dataset;
    //获取购物车数组;
    let { cart } = this.data;
    //找到修改商品的索引;
    const index = cart.findIndex((v) => v.goods_id === id);
    //判断是不是需要删除;
    if (cart[index].num === 1 && operation === -1) {
      //弹窗提示;
      const res = await showModal({
        content: "您是否需要删除",
      });
      if (res.confirm) {
        cart.splice(index, 1);
        this.setCart(cart);
      }
    } else {
      cart[index].num += operation;
      this.setCart(cart);
    }
  },
  async handlePay() {
    //判断收货地址;
    const { address, totalNum } = this.data;
    if (!address.userName) {
      await showToast({
        title: "您还没有选择收货地址",
      });
      return;
    }
    //判断用户有没有选择商品
    if (totalNum === 0) {
      await showToast({
        title: "您还没有选购商品",
      });
      return;
    }
    //跳转到支付页面;
    wx.navigateTo({
      url: "/pages/pay/pay",
    });
  },
});
